package com.itheima.bos.web.action.system;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheima.bos.domain.system.Menu;
import com.itheima.bos.domain.system.User;
import com.itheima.bos.service.system.MenuService;
import com.itheima.bos.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;

/*
 * 菜单
 */
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
@Controller
public class MenuAction extends BaseAction<Menu> {

	@Autowired
	private MenuService menuService;

	/*
	 * 查询所有的菜单
	 */
	@Action(value = "menu_list", results = { @Result(name = "success", type = "json") })
	public String list() {

		List<Menu> menus = menuService.findByParentMenuIsNull();

		ActionContext.getContext().getValueStack().push(menus);
		return SUCCESS;
	}

	/*
	 * 保存菜单的方法
	 */
	@Action(value = "menu_save", results = {
			@Result(name = "success", type = "redirect", location = "/pages/system/menu.html") })
	public String save() {

		menuService.save(model);

		return SUCCESS;
	}

	/*
	 * 根据当前用户来显示菜单
	 */
	@Action(value = "menu_showmenu", results = { @Result(name = "success", type = "json") })
	public String showMenu() {
		
		/*
		 * 获取当前用户
		 */
		Subject subject = SecurityUtils.getSubject();
		User user = (User)subject.getPrincipal();
		
		List<Menu> menus2 = menuService.findByUser(user);
		ActionContext.getContext().getValueStack().push(menus2);
		
		return SUCCESS;
	}

}
